import request from '../utils/request'

// 用户登录
export function login(data) {
  return request({
    url: '/users/login/',
    method: 'POST',
    data
  })
}

// 用户注册
export function register(data) {
  return request({
    url: '/users/register/',
    method: 'POST',
    data
  })
}

// 获取用户信息
export function getUserInfo() {
  return request({
    url: '/users/profile/',
    method: 'GET'
  })
}

// 更新用户信息
export function updateUserInfo(data) {
  return request({
    url: '/users/profile/',
    method: 'PUT',
    data
  })
}

// 获取用户详细信息
export function getUserProfileDetail() {
  return request({
    url: '/users/profile/detail/',
    method: 'GET'
  })
}

// 更新用户详细信息
export function updateUserProfileDetail(data) {
  return request({
    url: '/users/profile/detail/',
    method: 'PUT',
    data
  })
}

// 修改密码
export function changePassword(data) {
  return request({
    url: '/users/change-password/',
    method: 'POST',
    data
  })
}

// 登出
export function logout() {
  return request({
    url: '/users/logout/',
    method: 'POST'
  })
}

// 刷新token
export function refreshToken(data) {
  return request({
    url: '/users/token/refresh/',
    method: 'POST',
    data
  })
}

// 获取用户地址列表
export function getUserAddresses(params) {
  return request({
    url: '/users/addresses/',
    method: 'GET',
    params
  })
}

// 创建用户地址
export function createUserAddress(data) {
  return request({
    url: '/users/addresses/',
    method: 'POST',
    data
  })
}

// 获取用户地址详情
export function getUserAddressDetail(id) {
  return request({
    url: `/users/addresses/${id}/`,
    method: 'GET'
  })
}

// 更新用户地址
export function updateUserAddress(id, data) {
  return request({
    url: `/users/addresses/${id}/`,
    method: 'PUT',
    data
  })
}

// 删除用户地址
export function deleteUserAddress(id) {
  return request({
    url: `/users/addresses/${id}/`,
    method: 'DELETE'
  })
}

// 上传用户头像
export function uploadUserAvatar(file) {
  const formData = new FormData()
  formData.append('avatar', file)
  
  return request({
    url: '/users/avatar/upload/',
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}